import java.util.HashMap;
import java.util.Map;

/**
 * @author dx
 * @version 1.0
 * @date 2022/5/22 11:01
 * @description: 力扣1 两数之和
 */
public class TwoSum {
    public static void main(String[] args) {
        int []test = {2,7,11,15};
        int []ans = twoSum(test,9);
        for(int x:ans){
            System.out.println(x);
        }
    }
    public static int[] twoSum(int[] nums, int target) {
        int sum = 0;
        int []ans = new int[2];
        int a =0,b =0;
        for(int i =0;i<nums.length;i++){
            sum = target - nums[i];
            for(int j =i+1;j<nums.length;j++){
                if(sum - nums[j] == 0){
                    a = i;
                    b = j;
                    break;
                }
            }
        }
        ans[0] = a;ans[1] = b;
        return ans;
    }
    public static int[] twoSum2(int[] nums, int target){
        Map<Integer,Integer> map = new HashMap<>();
        for(int i =0;i<nums.length;i++){
            if(map.containsKey(target - nums[i])){
                return new int[]{map.get(target - nums[i]),map.get(nums[i])};
            }
            map.put(nums[i],i);
        }
        return new int[0];

    }
}
